package com.tushu.view.admin;

import javax.swing.JPanel;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import com.tushu.constants.Default;
import com.tushu.dao.StudentDao;
import com.tushu.utils.DataUtils;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.event.ActionEvent;

public class StudentManage extends JPanel implements Default{
	private JTextField jt1;
	private JTable table;
	private JTextField jt2;
	private JTextField jt3;
	private JPasswordField jt4;
	private JRadioButton rb_account,rb_name;
	
	private StudentDao studentDao=new StudentDao();
	
	String[] headStrings= {"序号","ID","账号","姓名"};
	
	String sql="select * from tb_student";
	
	String sql2 = "select * from tb_student where account=?";
	
	String sql3 = "select * from tb_student where name=?";

	String sql4 = "UPDATE tb_student SET account=?,`name`=? WHERE id=?";

	String sql5 = "UPDATE tb_student SET account=?,`name`=?,`password`=? WHERE id=?";

	String sql6 = "INSERT INTO tb_student VALUES(0,?,?,?)";


	/**
	 * Create the panel.
	 */
	public StudentManage() {
		setLayout(null);
		
		JButton jb_search = new JButton("\u67E5\u8BE2");
		jb_search.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				getAll(jt1.getText());
			}
		});
		jb_search.setBounds(335, 10, 97, 23);
		add(jb_search);
		
		jt1 = new JTextField();
		jt1.setBounds(130, 11, 195, 21);
		add(jt1);
		jt1.setColumns(10);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(24, 67, 480, 225);
		add(scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		
		rb_account = new JRadioButton("账号",true);
		rb_account.setBounds(164, 38, 66, 23);
		add(rb_account);
		
		rb_name = new JRadioButton("姓名");
		rb_name.setBounds(245, 39, 127, 23);
		add(rb_name);
		
		ButtonGroup buttonGroup=new ButtonGroup();
		buttonGroup.add(rb_account);
		buttonGroup.add(rb_name);
		
		jt2 = new JTextField();
		jt2.setBounds(68, 302, 66, 21);
		add(jt2);
		jt2.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("\u8D26\u53F7\uFF1A");
		lblNewLabel.setBounds(24, 305, 58, 15);
		add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("\u59D3\u540D\uFF1A");
		lblNewLabel_1.setBounds(144, 305, 58, 15);
		add(lblNewLabel_1);
		
		jt3 = new JTextField();
		jt3.setColumns(10);
		jt3.setBounds(186, 302, 66, 21);
		add(jt3);
		
		JLabel lblNewLabel_2 = new JLabel("\u5BC6\u7801\uFF1A");
		lblNewLabel_2.setBounds(262, 305, 66, 15);
		add(lblNewLabel_2);
		
		JButton jb_add = new JButton("\u65B0\u589E");
		jb_add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String account = jt2.getText();
				String name = jt3.getText();
				String password = String.valueOf(jt4.getPassword());
				try {
					int update = DataUtils.Update(sql6, new Object[]{account, name, password});
					if (update == 1) {
						JOptionPane.showMessageDialog(null, "新增成功！");
						getAll(null);
					} else {
						JOptionPane.showMessageDialog(null, "新增失败！");
					}
				} catch (SQLException ex) {
					throw new RuntimeException(ex);
				}

			}
		});
		jb_add.setBounds(407, 301, 97, 23);
		add(jb_add);
		
		JButton jb_update = new JButton("\u4FEE\u6539");
		jb_update.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int row = table.getSelectedRow();
				int id = (int) table.getValueAt(row, 1);
				String account = jt2.getText();
				String name = jt3.getText();
				String password = String.valueOf(jt4.getPassword());
				if (DEFAULT_INIT_PASSWORD.equals(password)) {
//					不更新密码
					try {
						int update = DataUtils.Update(sql4, new Object[]{account, name, id});
						if (update == 1) {
							JOptionPane.showMessageDialog(null, "更新成功！");
							getAll(null);
						} else {
							JOptionPane.showMessageDialog(null, "更新失败！");
						}
					} catch (SQLException ex) {
						throw new RuntimeException(ex);
					}
				} else {
					try {
						int update = DataUtils.Update(sql5, new Object[]{account, name, password, id});
						if (update == 1) {
							JOptionPane.showMessageDialog(null, "更新成功！");
							getAll(null);
						} else {
							JOptionPane.showMessageDialog(null, "更新失败！");
						}
					} catch (SQLException ex) {
						throw new RuntimeException(ex);
					}
				}
			}
		});
		jb_update.setBounds(24, 333, 97, 23);
		add(jb_update);
		
		JButton jb_delete = new JButton("\u5220\u9664");
		jb_delete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int row=table.getSelectedRow();
				String account=(String) table.getValueAt(row, 2);
				int res=studentDao.deleteStudent(account);
				if (res>0) {
					JOptionPane.showMessageDialog(null, "删除"+account+"成功！");
					getAll(null);
				}else {
					JOptionPane.showMessageDialog(null,"操作失败！");
				}
			}
		});
		jb_delete.setBounds(407, 333, 97, 23);
		add(jb_delete);
		
		jt4 = new JPasswordField(DEFAULT_INIT_PASSWORD);
		jt4.setBounds(297, 303, 79, 21);
		add(jt4);
		
		jt2.setEditable(true);
		jt3.setEditable(true);
		jt4.setEditable(true);
		
		JButton jb_reset = new JButton("\u91CD\u7F6E");
		jb_reset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getAll(null);
			}
		});
		jb_reset.setBounds(439, 10, 65, 23);
		add(jb_reset);
		getAll(null);
		table.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				int row=table.getSelectedRow();
				jt2.setText((String) table.getValueAt(row, 2));
				jt3.setText((String) table.getValueAt(row, 3));
			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	
	
	public void getAll(Object object) {
		if (null==object) {
			ResultSet set=null;
			try {
				set = DataUtils.query(sql, new Object[] {});
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				table.setModel(new DefaultTableModel(DataUtils.getSetArrays(set),headStrings));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else {
			if (rb_account.isSelected()) {
				try {
					table.setModel(new DefaultTableModel(DataUtils.getSetArrays(DataUtils.query(sql2, new Object[] {object})),headStrings));
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (rb_name.isSelected()) {
				try {
					table.setModel(new DefaultTableModel(DataUtils.getSetArrays(DataUtils.query(sql3, new Object[] {object})),headStrings));
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}
	
}
